package com.infitecs.eyas.mill;

import com.infitecs.eyas.expression.service.ExpressionService;
import com.infitecs.eyas.mill.model.Mill;
import com.infitecs.eyas.mill.model.MillSelect;
import com.infitecs.eyas.mill.model.MillSelectCalculate;
import com.infitecs.eyas.mill.service.MillSelectCalculateService;
import com.infitecs.eyas.mill.service.MillSelectService;
import com.infitecs.eyas.mill.service.MillService;
import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.dao.PowerDao;
import com.infitecs.eyas.power.model.OperateField;
import com.infitecs.eyas.power.model.Power;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.eyas.session.model.Param;
import com.infitecs.eyas.step.service.StepService;
import com.infitecs.eyas.util.ObjectUtil;
import com.infitecs.krill.ajax.response.AjaxResponse;
import com.infitecs.krill.ajax.response.ReturnState;
import com.infitecs.krill.exception.ValidateErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/6/28 0028.
 */
@Controller
@RequestMapping("/mill/calculate")
public class MillSelectCalculateController {
    @Autowired
    private MillSelectCalculateService millSelectCalculateService;

    @Autowired
    private SessionService sessionService;

    @Autowired
    private MillSelectService millSelectService;

    @Autowired
    private ExpressionService expressionService;

    @Autowired
    private MillService millService;

    @Autowired
    private StepService stepService;

    @Autowired
    private OperateFieldDao operateFieldDao;

    @Autowired
    private PowerDao powerDao;

    @RequestMapping("")
    public String getMillPage(HttpServletRequest request) {
        String cId = (String) request.getSession().getAttribute("caseId");
        if (cId == null) {
            return "mill/calculate";
        }
        int caseId = Integer.parseInt(cId);
        int step = stepService.getMzFxStep(caseId);
        //判断当前方案流程到哪里
        if (step < 6) {
            request.setAttribute("step", step);
            return "mill/calculate";
        }
        step = stepService.geMmjStep(caseId);
        if (step < 14) {
            request.setAttribute("step", step);
            return "mill/calculate";
        }
        Power powerTitle = powerDao.getCaseById(cId);
        OperateField operateField = operateFieldDao.getOperateField(caseId);
        MillSelect millSelect = millSelectService.getMillSelect();
        List<Mill> millList = millService.getMillList(millSelect.getE7().intValue());
        Map<String, Double>  dataMap = millSelectService.getChooseDataByCaseId(caseId);
        request.setAttribute("powerTitle", powerTitle);
        request.setAttribute("dataMap",dataMap);
        request.setAttribute("millSelect", millSelect);
        request.setAttribute("millList", millList);
        request.setAttribute("caseId", caseId);
        request.setAttribute("operateField", operateField);
        request.setAttribute("message", request.getParameter("message"));
        return "mill/calculate";
    }

    @RequestMapping(value = "/saveValueOfSelectCalculate", method = {RequestMethod.POST})
    @ResponseBody
    public void saveValueOfSelectCalculate(Param param) {
        millSelectCalculateService.saveValueOfSelectCalculate(param);
    }

    @RequestMapping(value = "/getCalculateInit", method = {RequestMethod.POST})
    @ResponseBody
    public void getCalculateInit() {
        sessionService.putSelectCalculateValue(sessionService.getCaseId());
    }

    @RequestMapping(value = "/getCalculate", method = RequestMethod.GET)
    @ResponseBody
    public MillSelectCalculate getCalculate() {
        int E7 = 0;
        MillSelectCalculate millSelectCalculate = new MillSelectCalculate();
        MillSelect millSelect = millSelectService.getMillSelect();
        if (millSelect != null) {
            E7 = millSelect.getE7().intValue();
        }
        millSelectCalculate.setE32(expressionService.calculate(new Param("AE32")));
        millSelectCalculate.setE10(expressionService.calculate(new Param("AE10")));
        millSelectCalculate.setE33(expressionService.calculate(new Param("AE33")));
        Map<String, Double> map = sessionService.getParamMap();
        Mill mill = millService.getMill(millSelectCalculate.getE33(), E7);
        map.put("AE34", mill.getBaseOutput());
        millSelectCalculate.setE34(mill.getBaseOutput());
        switch (E7) {
            case 7:
                millSelectCalculate.setE13(expressionService.calculate(new Param("AE14")));
                millSelectCalculate.setE17(expressionService.calculate(new Param("AE18")));
                millSelectCalculate.setE21(expressionService.calculate(new Param("AE22")));
                break;
            case 8:
                millSelectCalculate.setE13(expressionService.calculate(new Param("AE15")));
                millSelectCalculate.setE17(expressionService.calculate(new Param("AE19")));
                millSelectCalculate.setE21(expressionService.calculate(new Param("AE23")));
                break;
            case 9:
                millSelectCalculate.setE13(expressionService.calculate(new Param("AE16")));
                millSelectCalculate.setE17(expressionService.calculate(new Param("AE20")));
                millSelectCalculate.setE21(expressionService.calculate(new Param("AE24")));
                break;
            default:
                millSelectCalculate.setE13(expressionService.calculate(new Param("AE13")));
                millSelectCalculate.setE17(expressionService.calculate(new Param("AE17")));
                millSelectCalculate.setE21(expressionService.calculate(new Param("AE21")));
                break;
        }
        millSelectCalculate.setE35(expressionService.calculate(new Param("AE35")));
        millSelectCalculate.setE36(expressionService.calculate(new Param("AE36")));
        millSelectCalculate.setE37(expressionService.calculate(new Param("AE37")));
        millSelectCalculate.setE38(expressionService.calculate(new Param("AE38")));
        millSelectCalculate.setModel(mill.getModel());
        millSelectCalculate.setE39(mill.getDrag());
        switch (E7) {
            case 8:
                millSelectCalculate.setE40(expressionService.calculate(new Param("AE41")));
                break;
            case 9:
                millSelectCalculate.setE40(expressionService.calculate(new Param("AE41")));
                break;
            default:
                millSelectCalculate.setE40(expressionService.calculate(new Param("AE40")));
        }
        millSelectCalculate.setE42(expressionService.calculate(new Param("AE42")));
        if (E7 == 8) {
            millSelectCalculate.setE43(mill.getCapacityDy());
            map.put("AE43", mill.getCapacityDy());
        } else {
            millSelectCalculate.setE43(mill.getCapacity());
            map.put("AE43", mill.getCapacity());
        }
        millSelectCalculate.setE44(expressionService.calculate(new Param("AE44")));
        millSelectCalculate.setE45(expressionService.calculate(new Param("AE45")));
        millSelectCalculate.setE46(expressionService.calculate(new Param("AE46")));
        millSelectCalculate.setE47(expressionService.calculate(new Param("AE47")));
        millSelectCalculateService.updateMill(millSelectCalculate);
        return millSelectCalculate;
    }

    @RequestMapping(value = "/millChange", method = RequestMethod.GET)
    @ResponseBody
    public MillSelectCalculate millChange(HttpServletRequest request) throws UnsupportedEncodingException {
        int E7 = 0;
        String model = new String(request.getParameter("model").getBytes("iso8859_1"), "UTF-8");
        MillSelectCalculate millSelectCalculate = new MillSelectCalculate();
        MillSelect millSelect = millSelectService.getMillSelect();
        if (millSelect != null) {
            E7 = millSelect.getE7().intValue();
        }
        millSelectCalculate.setE32(expressionService.calculate(new Param("AE32")));
        millSelectCalculate.setE10(expressionService.calculate(new Param("AE10")));
        Mill mill = millService.getMill(model, E7);
        Map<String, Double> map = sessionService.getParamMap();
        map.put("AE34", mill.getBaseOutput());
        millSelectCalculate.setE34(mill.getBaseOutput());
        millSelectCalculate.setE33(expressionService.calculate(new Param("AE33")));
        millSelectCalculate.setModel(model);
        millSelectCalculateService.updateMill(millSelectCalculate);
        expressionService.calculate(new Param("AE35"));
        switch (E7) {
            case 7:
                millSelectCalculate.setE13(expressionService.calculate(new Param("AE14")));
                millSelectCalculate.setE17(expressionService.calculate(new Param("AE18")));
                millSelectCalculate.setE21(expressionService.calculate(new Param("AE22")));
                break;
            case 8:
                millSelectCalculate.setE13(expressionService.calculate(new Param("AE15")));
                millSelectCalculate.setE17(expressionService.calculate(new Param("AE19")));
                millSelectCalculate.setE21(expressionService.calculate(new Param("AE23")));
                break;
            case 9:
                millSelectCalculate.setE13(expressionService.calculate(new Param("AE16")));
                millSelectCalculate.setE17(expressionService.calculate(new Param("AE20")));
                millSelectCalculate.setE21(expressionService.calculate(new Param("AE24")));
                break;
            default:
                millSelectCalculate.setE13(expressionService.calculate(new Param("AE13")));
                millSelectCalculate.setE17(expressionService.calculate(new Param("AE17")));
                millSelectCalculate.setE21(expressionService.calculate(new Param("AE21")));
                break;
        }
        expressionService.calculate(new Param("AE36"));
        expressionService.calculate(new Param("AE38"));
        expressionService.calculate(new Param("AE37"));
        millSelectCalculate.setE35(map.get("AE35"));
        millSelectCalculate.setE36(map.get("AE36"));
        millSelectCalculate.setE37(map.get("AE37"));
        millSelectCalculate.setE38(map.get("AE38"));
        millSelectCalculate.setModel(mill.getModel());
        millSelectCalculate.setE39(mill.getDrag());
        switch (E7) {
            case 8:
                millSelectCalculate.setE40(expressionService.calculate(new Param("AE41")));
                break;
            case 9:
                millSelectCalculate.setE40(expressionService.calculate(new Param("AE41")));
                break;
            default:
                millSelectCalculate.setE40(expressionService.calculate(new Param("AE40")));
        }
        if (E7 == 8) {
            millSelectCalculate.setE43(mill.getCapacityDy());
            map.put("AE43", mill.getCapacityDy());
        } else {
            millSelectCalculate.setE43(mill.getCapacity());
            map.put("AE43", mill.getCapacity());
        }
        expressionService.calculate(new Param("AE45"));
        expressionService.calculate(new Param("AE46"));
        expressionService.calculate(new Param("AE47"));
        expressionService.calculate(new Param("AE42"));
        millSelectCalculate.setE42(map.get("AE42"));
        millSelectCalculate.setE44(map.get("AE44"));
        millSelectCalculate.setE45(map.get("AE45"));
        millSelectCalculate.setE46(map.get("AE46"));
        millSelectCalculate.setE47(map.get("AE47"));

        return millSelectCalculate;
    }

    @RequestMapping(value = "calculateSave", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse calculateSave(MillSelect millSelect) {
        try {
            millSelectCalculateService.calculateSave(millSelect);
            return new AjaxResponse();
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof ValidateErrorException) {
                return new AjaxResponse(ReturnState.WARNING, e.getMessage());
            }
            return new AjaxResponse(ReturnState.ERROR, "请检查填写数据是否有误，操作失败");
        }
    }
}
